import { configInfo } from '@/api/config';
import { articleTypelist } from '@/api/articleType';
export default defineNuxtRouteMiddleware(async (to, from) => {
  const config = useConfig();
  const articleType = useArticleType();
  const defaultActive: any = useDefaultActive();
  // 判断是否为刷新页面
  if (to.path === from.path) {
    const configInfoRes = await configInfo();
    if (configInfoRes.status === 20000) {
      config.value = configInfoRes.data;
    } else {
      config.value = {};
    }
    const typelistRes = await articleTypelist();
    if (typelistRes.status === 20000) {
      articleType.value = typelistRes.data;
    } else {
      articleType.value = {};
    }
  }
  let title = '';
  if (to.path == '/') {
    title = config.value.title || '';
    title += articleType.value[0].seo_title || '';
    defaultActive.value = '0';
  } else {
    if (!defaultActive.value) {
      defaultActive.value = to.params.identification;
    }
    const typeIndexArr = defaultActive.value.split('-');
    title = config.value.title || '';
    if (articleType.value[typeIndexArr[0]].seo_title) {
      title += '-' + articleType.value[typeIndexArr[0]].seo_title;
    }
    if (articleType.value[typeIndexArr[0]].children[typeIndexArr[1]].seo_title) {
      title += '-' + articleType.value[typeIndexArr[0]].children[typeIndexArr[1]].seo_title;
    }
  }
  useHead({
    title: title,
  });
  //   console.log('要去那个页面:' + to.path);
  //   console.log('来自那个页面:' + from.path);
  // abortNavigation()  //停止当前导航，可以使用error进行报错
  //  return  navigateTo('/')
});
